﻿<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
             xmlns:local="clr-namespace:OpenLightGroup.Common.UserManagement"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:System="clr-namespace:System;assembly=mscorlib"
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:olgBehaviors="clr-namespace:OpenLightGroup.Common.Interactivity;assembly=OpenLightGroup.Common.Interactivity"
             xmlns:olgConverters="clr-namespace:OpenLightGroup.Common.Converters;assembly=OpenLightGroup.Common.Interactivity"
             x:Name="userControl"
             x:Class="OpenLightGroup.Common.UserManagement.UserManagementControl"
             mc:Ignorable="d"
             d:DesignHeight="543"
             d:DesignWidth="544"
             MinWidth="545"
             MinHeight="545">
    <UserControl.Resources>
    	<olgConverters:StringArrayToStringConverter x:Key="StringArrayToStringConverter"/>
        <olgConverters:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter" />
        <olgConverters:ListCountVisibilityConverter x:Key="ListCountVisibilityConverter" />
        <DataTemplate x:Key="UserListBoxItemDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock x:Name="textBlock"
                           TextWrapping="Wrap"
                           Text="{Binding UserName}" />
                <TextBlock x:Name="textBlock1"
                           TextWrapping="Wrap"
                           Text="{Binding Email}"
                           Margin="7,0,0,0" />
                <TextBlock x:Name="textBlock2"
                           TextWrapping="Wrap"
                           Text="{Binding Roles, Converter={StaticResource StringArrayToStringConverter}}"
                           Margin="10,0,0,0" />
            </StackPanel>
        </DataTemplate>
        <Style TargetType="TextBlock"
               x:Key="UserEditorTextBlock">
            <Setter Property="VerticalAlignment"
                    Value="Center" />
            <Setter Property="HorizontalAlignment"
                    Value="Left" />
            <Setter Property="Margin"
                    Value="3,0,0,0" />
        </Style>
        <Style TargetType="TextBox"
               x:Key="UserEditorTextBox">
            <Setter Property="Margin"
                    Value="5,5,0,5" />
            <Setter Property="VerticalAlignment"
                    Value="Center" />
            <Setter Property="HorizontalAlignment"
                    Value="Left" />
            <Setter Property="Width"
                    Value="200" />
        </Style>
        <Style TargetType="PasswordBox"
               x:Key="UserEditorPasswordBox">
            <Setter Property="Margin"
                    Value="5,5,0,5" />
            <Setter Property="VerticalAlignment"
                    Value="Center" />
            <Setter Property="HorizontalAlignment"
                    Value="Left" />
            <Setter Property="Width"
                    Value="200" />
        </Style>
        <DataTemplate x:Key="RolesTreeViewDataTemplate">
            <Grid>
                <CheckBox x:Name="checkBox"
                          Content="{Binding Name}"
                          Margin="0,0,0,3"
                          d:LayoutOverrides="Width, Height" />
            </Grid>
        </DataTemplate>
        
    </UserControl.Resources>
    <i:Interaction.Triggers>
        <ei:PropertyChangedTrigger Binding="{Binding IsBusy, Mode=OneWay}">
            <i:Interaction.Behaviors>
                <ei:ConditionBehavior>
                    <ei:ConditionalExpression>
                        <ei:ComparisonCondition LeftOperand="{Binding IsBusy, Mode=OneWay}"
                                                RightOperand="True" />
                    </ei:ConditionalExpression>
                </ei:ConditionBehavior>
            </i:Interaction.Behaviors>
            <ei:GoToStateAction TargetObject="{Binding ElementName=userControl}"
                                StateName="IsBusy" />
        </ei:PropertyChangedTrigger>
        <ei:PropertyChangedTrigger Binding="{Binding IsBusy, Mode=OneWay}">
            <i:Interaction.Behaviors>
                <ei:ConditionBehavior>
                    <ei:ConditionalExpression>
                        <ei:ComparisonCondition LeftOperand="{Binding IsBusy, Mode=OneWay}"
                                                RightOperand="False" />
                    </ei:ConditionalExpression>
                </ei:ConditionBehavior>
            </i:Interaction.Behaviors>
            <ei:GoToStateAction TargetObject="{Binding ElementName=userControl}"
                                StateName="NotBusy" />
        </ei:PropertyChangedTrigger>
        <ei:PropertyChangedTrigger Binding="{Binding HasError}">
            <i:Interaction.Behaviors>
                <ei:ConditionBehavior>
                    <ei:ConditionalExpression>
                        <ei:ComparisonCondition LeftOperand="{Binding HasError}"
                                                RightOperand="True" />
                    </ei:ConditionalExpression>
                </ei:ConditionBehavior>
            </i:Interaction.Behaviors>
            <ei:GoToStateAction TargetObject="{Binding ElementName=userControl}"
                                StateName="HasError" />
        </ei:PropertyChangedTrigger>
    </i:Interaction.Triggers>
    <UserControl.DataContext>
        <local:UserManagementViewModel />
    </UserControl.DataContext>

    <Grid x:Name="LayoutRoot"
          Background="White">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="IsBusyStates">
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0:0:0.1" />
                </VisualStateGroup.Transitions>
                <VisualState x:Name="NotBusy" />
                <VisualState x:Name="IsBusy">
                    <Storyboard>
                        <DoubleAnimation Duration="0"
                                         To="0.15"
                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                         Storyboard.TargetName="LayoutRoot"
                                         d:IsOptimized="True" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="ErrorStates">
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0:0:0.1" />
                    <VisualTransition GeneratedDuration="0:0:0.1"
                                      To="HasError">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                           Storyboard.TargetName="ErrorBorder">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Visible</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualTransition>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="HasError">
                    <Storyboard>
                        <DoubleAnimation Duration="0"
                                         To="0"
                                         Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)"
                                         Storyboard.TargetName="ErrorBorder"
                                         d:IsOptimized="True" />
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="ErrorBorder">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="NoError" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>


        <i:Interaction.Triggers>
            <i:EventTrigger>
                <i:InvokeCommandAction Command="{Binding GetRolesCommand, Mode=OneWay}" />
            </i:EventTrigger>

        </i:Interaction.Triggers>
        <sdk:TabControl x:Name="MainTabControl"
                        Margin="0"
                        d:LayoutOverrides="Width">
            <sdk:TabItem x:Name="CreateUserTabItem"
                         Header="Create User">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <local:UserEditor Margin="0" />
                    <Button x:Name="CreateUserButton"
                            Content="Create User"
                            HorizontalAlignment="Right"
                            Margin="0,8,5,0"
                            Grid.Row="1"
                            VerticalAlignment="Top"
                            Width="75"
                            Height="25"
                            Command="{Binding CreateUserCommand, Mode=OneWay}" />
                    <sdk:ValidationSummary x:Name="CreateUserValidationSummary"
                                           VerticalAlignment="Bottom"
                                           Margin="0"
                                           Grid.Row="1" />
                </Grid>
            </sdk:TabItem>
            <sdk:TabItem x:Name="ManageUsersTabItem"
                         Header="Manage Users">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.493*" />
                        <ColumnDefinition Width="0.507*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="164" />
                        <RowDefinition Height="36"/>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel HorizontalAlignment="Left"
                                Margin="0,7"
                                Orientation="Horizontal"
                                Grid.ColumnSpan="2">
                        <TextBlock Margin="0"
                                   TextWrapping="Wrap"
                                   Text="Search by:"
                                   VerticalAlignment="Center" />
                        <ComboBox x:Name="SearchTypeComboBox"
                                  Width="120"
                                  Height="25"
                                  Margin="5,0,0,0"
                                  SelectedItem="{Binding SearchType, Mode=TwoWay}">
                            <System:String>Username</System:String>
                            <System:String>Email</System:String>
                        </ComboBox>
                        <TextBlock TextWrapping="Wrap"
                                   Text="for:"
                                   VerticalAlignment="Center"
                                   Margin="5,0,0,0" />
                        <TextBox x:Name="SearchParameterTextBox"
                                 Margin="5,0,0,0"
                                 TextWrapping="Wrap"
                                 Width="130"
                                 VerticalAlignment="Center"
                                 Text="{Binding SearchTerm, Mode=TwoWay}">
                            <i:Interaction.Behaviors>
                                <olgBehaviors:SelectTextOnFocusBehavior />
                            </i:Interaction.Behaviors>
                        </TextBox>
                        <Button x:Name="FindUserButton"
                                Content="Find User"
                                Width="75"
                                Margin="5,0,0,0"
                                Command="{Binding FindUserCommand, Mode=OneWay}" />
                    </StackPanel>
                    <ListBox x:Name="UsersListBox"
                             Margin="5,5,5,0"
                             Grid.Row="1"
                             ItemsSource="{Binding Users}"
                             ItemTemplate="{StaticResource UserListBoxItemDataTemplate}"
                             Grid.ColumnSpan="2" SelectedItem="{Binding CurrentUser, Mode=TwoWay}" />
                    <Button x:Name="SaveUserButton"
                            Content="Save"
                            HorizontalAlignment="Right"
                            Margin="0,5,5,0"
                            Grid.Row="4"
                            Width="75"
                            VerticalAlignment="Top"
                            Height="25"
                            Command="{Binding SaveChangesCommand, Mode=OneWay}"
                            Grid.Column="1" />
                    <Grid x:Name="EditUserGrid"
                          Margin="0"
                          Grid.Row="3"
                          DataContext="{Binding SelectedItem, ElementName=UsersListBox, Mode=TwoWay}"
                          Grid.ColumnSpan="2">

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto"/>

                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto"
                                              MinWidth="200" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <TextBlock x:Name="UsernameTextBlock"
                                   Text="Username:"
                                   Style="{StaticResource UserEditorTextBlock}" />
                        <TextBox x:Name="UsernameTextBox"
                                 Grid.Column="1"
                                 Style="{StaticResource UserEditorTextBox}"
                                 HorizontalAlignment="Stretch"
                                 Margin="5"
                                 Width="Auto"
                                 Text="{Binding UserName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
                                 IsReadOnly="True" />
                        <TextBlock x:Name="EmailAddressTextBlock"
                                   Text="Email:"
                                   Grid.Row="1"
                                   Style="{StaticResource UserEditorTextBlock}" />
                        <TextBox x:Name="EmailAddressTextBox"
                                 Grid.Row="1"
                                 Grid.Column="1"
                                 Style="{StaticResource UserEditorTextBox}"
                                 HorizontalAlignment="Stretch"
                                 Margin="5"
                                 Width="Auto"
                                 Text="{Binding Email, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
                        <CheckBox x:Name="checkBox" Content="Is Active" Margin="4,0,0,0" Grid.Row="2" VerticalAlignment="Bottom" d:LayoutOverrides="Width" IsChecked="{Binding Enabled, Mode=TwoWay}" Grid.Column="1"/>
                    </Grid>
                    <sdk:ValidationSummary Margin="0"
                                           Grid.Row="4"
                                           VerticalAlignment="Bottom"
                                           d:LayoutOverrides="Width"
                                           Grid.ColumnSpan="2" />
                    <HyperlinkButton Content="Change Password" Margin="5,0,0,0" Grid.Row="2" d:LayoutOverrides="Width" Height="16" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="{Binding PasswordRetrievalEnabled, Converter={StaticResource BooleanVisibilityConverter}}">
                    	<i:Interaction.Triggers>
                    		<i:EventTrigger EventName="Click">
                    			<olgBehaviors:ShowChildWindowAction ClosedCommand="{Binding ChangePasswordCommand, Mode=OneWay}" CommandParameter="{Binding NewPassword, Mode=TwoWay}">
                    				<olgBehaviors:ShowChildWindowAction.ChildWindow>
                    					<local:ChangePasswordChildWindow DataContext="{Binding}"  />
                    				</olgBehaviors:ShowChildWindowAction.ChildWindow>
                    			</olgBehaviors:ShowChildWindowAction>
                    		</i:EventTrigger>
                    	</i:Interaction.Triggers>
                    </HyperlinkButton>
                    <local:UserRolesManager Margin="5"
                                            Grid.Row="3"
                                            d:LayoutOverrides="Width"
                                            Grid.Column="1"
                                            Height="125"
                                            User="{Binding SelectedItem, ElementName=UsersListBox}" />
                </Grid>
            </sdk:TabItem>
            <sdk:TabItem x:Name="ManageRolesTabItem"
                         Header="Create or Manage Roles">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="0.825*" />
                        <RowDefinition Height="0.095*" />
                    </Grid.RowDefinitions>
                    <StackPanel Margin="5,8,239,9"
                                Orientation="Horizontal"
                                d:LayoutOverrides="Width, Height">
                        <TextBlock Margin="0"
                                   TextWrapping="Wrap"
                                   Text="New role name:"
                                   VerticalAlignment="Center" />
                        <TextBox x:Name="NewRoleTextBox"
                                 TextWrapping="Wrap"
                                 Width="200"
                                 VerticalAlignment="Center"
                                 Text="{Binding NewRole.Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}">
                            <i:Interaction.Behaviors>
                                <olgBehaviors:SelectTextOnFocusBehavior />
                            </i:Interaction.Behaviors>
                        </TextBox>
                    </StackPanel>
                    <Button x:Name="AddRoleButton"
                            Content="Add Role"
                            Width="75"
                            Margin="0,0,5,0"
                            VerticalAlignment="Center"
                            Height="25"
                            Command="{Binding AddRoleCommand, Mode=OneWay}"
                            CommandParameter="{Binding Text, ElementName=NewRoleTextBox}"
                            HorizontalAlignment="Right" />
                    <ListBox x:Name="RolesListBox"
                             Margin="5"
                             Grid.Row="1"
                             ItemsSource="{Binding Roles}" />
                    <Button x:Name="DeleteRoleButton"
                            Content="Delete Selected Role"
                            HorizontalAlignment="Right"
                            Margin="0,5,5,5"
                            Grid.Row="2"
                            VerticalAlignment="Center"
                            Height="25"
                            Command="{Binding DeleteRoleCommand, Mode=OneWay}"
                            CommandParameter="{Binding SelectedItem, ElementName=RolesListBox}" />
                </Grid>
            </sdk:TabItem>
        </sdk:TabControl>
        <Border x:Name="ErrorBorder"
                BorderBrush="Black"
                BorderThickness="1"
                Margin="5"
                CornerRadius="5"
                Background="#E6D0D0D0"
                RenderTransformOrigin="0.5,0.5"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                Padding="10"
                Visibility="Collapsed">
            <Border.RenderTransform>
                <CompositeTransform TranslateX="-560" />
            </Border.RenderTransform>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.058*" />
                    <RowDefinition Height="0.869*" />
                    <RowDefinition Height="0.073*" />
                </Grid.RowDefinitions>
                <TextBlock x:Name="ErrorMessageHeaderTextBlock"
                           TextWrapping="Wrap"
                           Text="An Error Has Occrured"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="21.333" />
                <TextBlock x:Name="ErrorMessageTextBlock"
                           HorizontalAlignment="Left"
                           Margin="8,25"
                           Grid.Row="1"
                           TextWrapping="Wrap"
                           Text="{Binding Error.Message, Mode=OneWay}"
                           VerticalAlignment="Top" />
                <Button x:Name="CloseErrorButton"
                        Content="OK"
                        Margin="0"
                        Grid.Row="2"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Width="75"
                        Height="25">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <ei:GoToStateAction StateName="NoError" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </Grid>
        </Border>

    </Grid>
</UserControl>
